package com.ggsound.bluetoothbox.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.provider.CalendarContract;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.View;

import com.ggsound.bluetoothbox.R;
import com.ggsound.bluetoothbox.cache.db.entity.FmChannelCache;
import com.ggsound.bluetoothbox.listener.FmItemDleteListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * @author 尹晓童
 * 邮箱：yinxtno1@yeah.net
 * 时间：2017/11/22
 * 作用：Fm频道列表适配器
 */

public class FmChannelAdapter extends BaseQuickAdapter<FmChannelCache, BaseViewHolder> {

    private Context mContext;
    private FmItemDleteListener listener;

    public FmChannelAdapter(Context context, @Nullable List<FmChannelCache> data) {
        super(R.layout.item_fm, data);
        mContext = context;

    }
    private int selectedIndex = -1;        //记录当前选中的条目索引

    public void setSelectedIndex(int position) {
        if (selectedIndex!=-1){
            notifyItemChanged(selectedIndex);
        }
        this.selectedIndex = position;
        notifyItemChanged(position);
    }
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void convert(BaseViewHolder helper, FmChannelCache item) {
        int layoutPosition = helper.getLayoutPosition();
        helper.setText(R.id.fm_name,"ST "+String.valueOf(layoutPosition+1));
               helper.setText(R.id.fm_channel, mContext.getString(R.string.fm_channel, item.getChannel() / 1000f));

        helper.getView(R.id.fm_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//刪除該選項
                listener.delete(helper.getAdapterPosition());
            }
        });

        if (selectedIndex == helper.getPosition()) {
            //选中状态
            helper.setTextColor(R.id.fm_name, mContext.getColor(R.color.list_select));
            helper.setTextColor(R.id.fm_channel,mContext.getColor(R.color.list_select));
        } else {                                                            //非选中状态
            helper.setTextColor(R.id.fm_name,Color.WHITE);
            helper.setTextColor(R.id.fm_channel,Color.WHITE);
        }

    }

    public void setFmitemDeleteListener(FmItemDleteListener listener) {
        this.listener = listener;
    }
}
